package com.showde.shoppingmall.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.db.ShoppingcartDBCommand;
import com.showde.shoppingmall.domain.ShoppingCart;
import com.showde.shoppingmall.utils.ConformDialogUtils;
import com.showde.shoppingmall.utils.ConformDialogUtils.DialogCallback;

public class ShoppingcartModifyAdapter extends BaseAdapter {

	private Context context;

	private List<ShoppingCart> shoppingCarts;
	private List<String> productIds;

	private ImageLoader imageLoader;

	private ShoppingcartDBCommand shoppingcartDBCommand;

	public ShoppingcartModifyAdapter(Context context,
			List<ShoppingCart> shoppingCarts) {
		this.context = context;
		this.shoppingCarts = shoppingCarts;
		imageLoader = new ImageLoader(context);
		shoppingcartDBCommand = new ShoppingcartDBCommand(context);
		updateProductIds();
	}

	public void resetData(List<ShoppingCart> shoppingCarts) {
		this.shoppingCarts = shoppingCarts;
		updateProductIds();
	}

	private void updateProductIds() {
		productIds = new ArrayList<String>();
		if (shoppingCarts.size() == 0) {
			return;
		}
		for (ShoppingCart shoppingCart : shoppingCarts) {
			productIds.add(shoppingCart.getProductId());
		}
	}

	@Override
	public int getCount() {
		if (shoppingCarts.size() == 0) {
			return 0;
		}
		return shoppingCarts.size();
	}

	@Override
	public ShoppingCart getItem(int position) {
		return shoppingCarts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_shoppingcart_pro_midofy, null);
			holder = new ViewHolder();
			holder.porductLogo = (ImageView) convertView
					.findViewById(R.id.img_product_icon);
			holder.productTitle = (TextView) convertView
					.findViewById(R.id.txt_pro_desc);
			holder.productReduce = (ImageView) convertView
					.findViewById(R.id.img_reducepro_count);
			holder.productIncrease = (ImageView) convertView
					.findViewById(R.id.img_increasepro_count);
			holder.productCount = (EditText) convertView
					.findViewById(R.id.edt_pro_count);
			holder.productDelete = (ImageView) convertView
					.findViewById(R.id.img_delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		ShoppingCart shoppingCart = getItem(position);
		final String productId = shoppingCart.getProductId();
		final int productCount = shoppingCart.getProductCount();
		holder.productTitle.setText(shoppingCart.getProductTitle());
		final EditText editText = holder.productCount;
		holder.productCount.setText(String.valueOf(shoppingCart
				.getProductCount()));
		holder.productReduce.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				reduceProduct(position, productId, productCount, editText);
			}
		});

		holder.productIncrease.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				increaseProduct(productId, productCount, editText);
			}
		});

		holder.productCount.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				updateProductCount(productId, productCount, s.toString());
			}
		});

		holder.productDelete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				removeFromShoppingcart(position, productId, false);
			}
		});

		imageLoader.displayImage(shoppingCart.getProductLogo(),
				holder.porductLogo);

		return convertView;
	}

	/**
	 * 增加数量
	 * 
	 * @param productId
	 * @param productCount
	 */
	private void increaseProduct(String productId, int productCount,
			EditText editText) {
		int count = productCount + 1;
		shoppingcartDBCommand.updateShoppingcartCount(productId, count);
		shoppingCarts = shoppingcartDBCommand
				.queryShoppingcartByProductIdArray(productIds);
		notifyDataSetChanged();
	}

	/**
	 * 减少数量
	 * 
	 * @param productId
	 * @param productCount
	 */
	private void reduceProduct(final int position, final String productId,
			int productCount, EditText editText) {
		int count = productCount - 1;
		if (count == 0) {
			ConformDialogUtils.getInstance(context).showConformDialog(
					"确定将该商品从购物车删除?", new DialogCallback() {

						@Override
						public void onPositiveCall() {
							removeFromShoppingcart(position, productId, true);
						}
					});
			return;
		}
		shoppingcartDBCommand.updateShoppingcartCount(productId, count);
		shoppingCarts = shoppingcartDBCommand
				.queryShoppingcartByProductIdArray(productIds);
		notifyDataSetChanged();
	}

	private void updateProductCount(String productId, int productCount,
			String productCountStr) {
		int productCountTemp = productCount;
		try {
			productCountTemp = Integer.parseInt(productCountStr);
		} catch (Exception e) {
		}
		shoppingcartDBCommand.updateShoppingcartCount(productId,
				productCountTemp);
		shoppingCarts = shoppingcartDBCommand
				.queryShoppingcartByProductIdArray(productIds);
		notifyDataSetChanged();
	}

	/**
	 * 从购物车删除
	 * 
	 * @param position
	 * @param productId
	 */
	private void removeFromShoppingcart(final int position,
			final String productId, boolean conformed) {

		if (!conformed) {
			ConformDialogUtils.getInstance(context).showConformDialog(
					"确定将该商品从购物车删除?", new DialogCallback() {
						@Override
						public void onPositiveCall() {
							shoppingcartDBCommand.delete(productId);
							shoppingCarts.remove(position);
							updateProductIds();
							notifyDataSetChanged();
						}
					});
			return;
		}

		shoppingcartDBCommand.delete(productId);
		shoppingCarts.remove(position);
		updateProductIds();
		notifyDataSetChanged();
	}

	AlertDialog alertDialog = null;

	private final class ViewHolder {
		ImageView porductLogo;
		TextView productTitle;
		ImageView productReduce;
		ImageView productIncrease;
		EditText productCount;
		ImageView productDelete;
	}

}
